package com.aiju.cash.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.bean.Shop;
import com.aiju.cash.R;
import com.aiju.cash.core.api.BlotterApi;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.ui.main.adapter.SelectEmployAdapter;
import com.aiju.cash.ui.main.entity.SelectEmployy;
import com.aiju.cash.ui.main.view.BlotterSearchBar;
import com.aiju.cash.ui.main.view.CharacterParser;
import com.aiju.cash.ui.main.view.PinyinComparator;
import com.aiju.support.recyclerView.DividerItemDecoration;
import com.aiju.utils.T;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * 选择员工
 */

public class SelectEmployeeActivity extends BaseTitleActivity implements BlotterSearchBar.SeachBarListener, SelectEmployAdapter.OnItemClickListener {

    private RecyclerView recyclerviewTeams;
    private LinearLayoutManager mTeamsLayoutManager;
    private SelectEmployAdapter selectEmployAdapter;
    private BlotterSearchBar blotterSearchBar;
    private BlotterApi mBlotterApi;

    List<SelectEmployy> list = new ArrayList<>();
    List<SelectEmployy> cloneList = new ArrayList<>();
    private String storeId;

    /**
     * 汉字转换成拼音的类
     */
    private CharacterParser characterParser;
    private List<Shop> newShopList;
    /**
     * 根据拼音来排列ListView里面的数据类
     */
    private PinyinComparator pinyinComparator;

    private String selectOperatorId = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_shop);
        //实例化汉字转拼音类
        characterParser = CharacterParser.getInstance();

        pinyinComparator = new PinyinComparator();
        selectOperatorId = getIntent().getStringExtra("selectOperatorId");
        initView();
        findViewById(R.id.sryt_swipe_listview).setEnabled(false);
    }


    private void initView() {
        setCenterTitle("选择店铺成员");
        mAiJuTitleBarView.setRightTitle("确认");
        mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2017/6/17 返回选择店铺信息
                SelectEmployy selectedShop = selectEmployAdapter.getSelectedShop();
                if (selectedShop == null) {
                    T.ToastShort("请选择店员");
                    return;
                }
                String userName = selectedShop.getUserName();
                String operatorId = selectedShop.getOperatorId();
                Intent intent = new Intent();
                intent.putExtra("userName", userName);
                intent.putExtra("operatorId", operatorId);
                intent.putExtra("storeId",selectedShop.getStoreId());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        recyclerviewTeams = (RecyclerView) findViewById(R.id.recyclerview_teams);
        blotterSearchBar = (BlotterSearchBar) findViewById(R.id.search_bar);
        blotterSearchBar.setLIster(this);
        blotterSearchBar.setEdiHint("请输入成员名称");
        mTeamsLayoutManager = new LinearLayoutManager(this);
        recyclerviewTeams.setLayoutManager(mTeamsLayoutManager);
        recyclerviewTeams.addItemDecoration(new DividerItemDecoration(this,
                DividerItemDecoration.VERTICAL_LIST));
        mBlotterApi = new BlotterApi();
        Intent intent = getIntent();
        if (intent != null) {
            storeId = intent.getStringExtra("storeId");
        }
        getBlotterList();

    }


    private void getBlotterList() {

        mBlotterApi.queryClerkList(mUser, storeId, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                T.ToastNetError();
            }

            @Override
            public void onResponse(String response, int id) {
                mDialogHelper.dismissProgressDialog();
                DataJson json = new DataJson(response);
                if ("操作成功".equals(json.getMsg())) {

                    if (json.getData() != null) {
                        list = new Gson().fromJson(json.getData().toString(), new TypeToken<List<SelectEmployy>>() {
                        }.getType());
                        SelectEmployy selectEmployy = new SelectEmployy();
                        selectEmployy.setOperatorId("");
                        selectEmployy.setUserName("全部店员");
                        if (selectOperatorId.equals(selectEmployy.getOperatorId())) {
                            selectEmployy.setSelected(true);
                        }
                        cloneList.add(selectEmployy);
                        for (int i = 0; i < list.size(); i++) {
                            SelectEmployy selectEmployy1 = list.get(i);
                            if (selectOperatorId.equals(selectEmployy1.getOperatorId())) {
                                selectEmployy1.setSelected(true);
                            }
                            cloneList.add(selectEmployy1);
                        }
                        setBlotterListView(cloneList);
                    }
                } else {
                    T.ToastShort("网络异常");
                }

            }
        });
    }


    private void setBlotterListView(List<SelectEmployy> list) {
        if (selectEmployAdapter == null) {
            selectEmployAdapter = new SelectEmployAdapter(this, list);
            selectEmployAdapter.setOnItenClickListner(this);
            recyclerviewTeams.setVisibility(View.VISIBLE);
            recyclerviewTeams.setAdapter(selectEmployAdapter);
        } else {
            list.clear();
        }
    }

    @Override
    public void editextWaterValue(String value) {
        filterData(value);
    }

    @Override
    public void edutextWaterNoValue() {
        selectEmployAdapter.setList(cloneList);
    }

    @Override
    public void clearIamgeClick() {
        selectEmployAdapter.setList(cloneList);
    }


    /**
     * 根据输入框中的值来过滤数据并更新ListView
     *
     * @param filterStr
     */
    private void filterData(String filterStr) {

        List<SelectEmployy> filterDateList = new ArrayList<>();
        SelectEmployy selectEmployy = new SelectEmployy();
        selectEmployy.setUserName("全部店员");
        selectEmployy.setOperatorId("");
        selectEmployy.setSelected(false);
        filterDateList.add(selectEmployy);
        if (TextUtils.isEmpty(filterStr)) {
            filterDateList = list;
        } else {
            for (SelectEmployy sortModel : list) {
                String name = sortModel.getUserName();
                if (name.toUpperCase().indexOf(
                        filterStr.toString().toUpperCase()) != -1
                        || characterParser.getSelling(name).toUpperCase()
                        .startsWith(filterStr.toString().toUpperCase())) {
                    filterDateList.add(sortModel);
                }
            }
        }
        selectEmployAdapter.setList(filterDateList);
    }

    @Override
    public void setOnItemClickListenering(String storeId) {
        for (int i = 0; i < cloneList.size(); i++) {
            cloneList.get(i).setSelected(false);
        }
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSelected(false);
        }
        for (int i = 0; i < cloneList.size(); i++) {
            if (cloneList.get(i).getOperatorId().equals(storeId)) {
                cloneList.get(i).setSelected(true);
                break;
            }
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getOperatorId().equals(storeId)) {
                list.get(i).setSelected(true);
                break;
            }
        }

    }
}
